package com.tsingtree.modular.controller.view;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tsingtree.base.auth.context.LoginContextHolder;
import com.tsingtree.base.auth.model.LoginUser;
import com.tsingtree.base.pojo.page.LayuiPageInfo;
import com.tsingtree.modular.entity.NanSellAnnounce;
import com.tsingtree.modular.entity.NanSellAnnounceComment;
import com.tsingtree.modular.entity.NanSellAnnounceReply;
import com.tsingtree.modular.enums.CheckStatusEnum;
import com.tsingtree.modular.model.params.NanSellAnnounceReplyParam;
import com.tsingtree.modular.service.NanSellAnnounceCommentService;
import com.tsingtree.modular.service.NanSellAnnounceReplyService;
import com.tsingtree.modular.service.NanSellAnnounceService;
import com.tsingtree.roses.core.base.controller.BaseController;
import com.tsingtree.roses.core.reqres.response.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Objects;


/**
 * 回复控制器
 *
 * @author jagger
 */
@Controller
@RequestMapping("/nanSellAnnounceReply")
public class NanSellAnnounceReplyController extends BaseController {

    private String PREFIX = "/nanSellAnnounceReply";

    @Autowired
    private NanSellAnnounceReplyService nanSellAnnounceReplyService;

    @Autowired
    private NanSellAnnounceService nanSellAnnounceService;

    @Autowired
    private NanSellAnnounceCommentService nanSellAnnounceCommentService;
    /**
     * 跳转到主页面
     *
     * @author jagger
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/nanSellAnnounceReply.html";
    }

    @RequestMapping("/reply")
    public String reply() {
        return PREFIX + "/nanSellAnnounceReply_reply.html";
    }

    /**
     * 新增页面
     *
     * @author jagger
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/nanSellAnnounceReply_add.html";
    }

    /**
     * 编辑页面
     *
     * @author jagger
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/nanSellAnnounceReply_edit.html";
    }

    /**
     * 删除接口
     *
     * @author jagger
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(NanSellAnnounceReplyParam nanSellAnnounceReplyParam) {
        NanSellAnnounceReply reply = nanSellAnnounceReplyService.getById(nanSellAnnounceReplyParam.getId());
        NanSellAnnounceComment nanSellAnnounceComment = nanSellAnnounceCommentService.getById(reply.getCommentId());
        LoginUser admin = LoginContextHolder.getContext().getUser();
        if(reply.getIsDel()){
            return ResponseData.error("该回复已被删除");
        }
        reply.setIsDel(true);
        NanSellAnnounce nanSellAnnounce = nanSellAnnounceService.getById(nanSellAnnounceComment.getAnnounceId());
        nanSellAnnounce.setCommentTimes(nanSellAnnounce.getCommentTimes() - 1);
        nanSellAnnounceReplyService.del(reply, nanSellAnnounce);
        return ResponseData.success();
    }

    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(NanSellAnnounceReplyParam nanSellAnnounceReplyParam) {
        NanSellAnnounceReply reply = new NanSellAnnounceReply();
        reply.setContent(nanSellAnnounceReplyParam.getContent());
        int announceId;
        if(Objects.nonNull(nanSellAnnounceReplyParam.getCommentId())){
            NanSellAnnounceComment comment = nanSellAnnounceCommentService.getById(nanSellAnnounceReplyParam.getCommentId());
            announceId = comment.getAnnounceId();
            reply.setReplyUserId(comment.getUserId());
            reply.setCommentId(comment.getId());
            reply.setAnnounceId(comment.getAnnounceId());
        } else{
            NanSellAnnounceReply nanSellAnnounceReply = nanSellAnnounceReplyService.getById(nanSellAnnounceReplyParam.getReplyId());
            if(Objects.isNull(nanSellAnnounceReply.getUserId())){
                return ResponseData.error("您不能回复自己");
            }
            announceId = nanSellAnnounceReply.getAnnounceId();
            reply.setReplyId(nanSellAnnounceReplyParam.getReplyId());
            reply.setReplyUserId(nanSellAnnounceReply.getUserId());
            reply.setCommentId(nanSellAnnounceReply.getCommentId());
            reply.setAnnounceId(nanSellAnnounceReply.getAnnounceId());
        }
        NanSellAnnounce sellAnnounce = nanSellAnnounceService.getOne(new QueryWrapper<NanSellAnnounce>().eq("id", announceId)
                .eq("is_del", false).eq("check_status", CheckStatusEnum.ON_LINE.getKey()));
        if(Objects.isNull(sellAnnounce)){
            return ResponseData.error("文章不存在");
        }
        sellAnnounce.setCommentTimes(sellAnnounce.getCommentTimes() + 1);
        this.nanSellAnnounceReplyService.saveReply(reply, sellAnnounce);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author jagger
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(NanSellAnnounceReplyParam nanSellAnnounceReplyParam) {
        NanSellAnnounceReply detail = this.nanSellAnnounceReplyService.getById(nanSellAnnounceReplyParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author jagger
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(NanSellAnnounceReplyParam nanSellAnnounceReplyParam) {
        return this.nanSellAnnounceReplyService.findPageBySpec(nanSellAnnounceReplyParam);
    }

}


